Imports System.Reflection
Imports System.Configuration
Imports System.Globalization

Public Class Common
    Private Const CONNECTION_STRING_KEY As String = "ConnectionString"
    Private Const DATE_FORMAT_KEY As String = "DateFormat"
    Private Const DESTINATION_PORT_LK_ID As String = "DestinationPortLKID"
    Private Const SORT_REF_LK_ID As String = "SortRefLKID"
    Private Const SORT_REF2_LK_ID As String = "SortRef2LKID"
    Private Const SORT_RANGE_LK_ID As String = "SortRangeLKID"
    Private Const PRODUCT_CODE_LK_ID As String = "ProductCodeLKID"
    Private Const MIMS_PRODUCT_CODE_LK_ID As String = "MIMSProductCodeLKID"
    Private Const PRODUCT_COLOR_LK_ID As String = "ProductColorLKID"
    Private Const SUPPLIER_CODE_LK_ID As String = "SupplierCodeLKID"
    Private Const COUNTRY_LK_ID As String = "UserCountryLKID"
    Private Const FREIGHT_FORWARDER_LK_ID As String = "FreightForwarderLKID"


    Public Const HashAlgorithm As String = "md5" '"sha1" 'vbNullString '

#Region "Properties"
    'Public Shared ReadOnly Property ApplicationRoot() As String
    '    Get
    '        Try
    '            Return ConfigurationSettings.AppSettings("MIMS_WRS.ApplicationRoot")
    '        Catch ex As Exception
    '            Return Nothing
    '        End Try

    '    End Get
    'End Property
#End Region

#Region " Methods"
    Public Shared Function GetRandomFileName() As String
        Dim sFN As String

        sFN = sFN & Year(Now())
        sFN = sFN & Format(Month(Now()), "00")
        sFN = sFN & Format(Day(Now()), "00")
        sFN = sFN & Format(Hour(Now), "00")
        sFN = sFN & Format(Minute(Now()), "00")
        sFN = sFN & Format(Second(Now()), "00")

        GetRandomFileName = sFN
    End Function


    Public Shared Function GetConnectionString() As String
        Return System.Configuration.ConfigurationSettings.AppSettings(CONNECTION_STRING_KEY)
    End Function

    Public Shared Function GetDestinationPortLKID() As Integer
        Return System.Configuration.ConfigurationSettings.AppSettings(DESTINATION_PORT_LK_ID)
    End Function

    Public Shared Function GetSortRefLKID() As Integer
        Return System.Configuration.ConfigurationSettings.AppSettings(SORT_REF_LK_ID)
    End Function

    Public Shared Function GetSortRef2LKID() As Integer
        Return System.Configuration.ConfigurationSettings.AppSettings(SORT_REF2_LK_ID)
    End Function

    Public Shared Function GetSortRangeLKID() As Integer
        Return System.Configuration.ConfigurationSettings.AppSettings(SORT_RANGE_LK_ID)
    End Function

    Public Shared Function GetProductCodeLKID() As Integer
        Return System.Configuration.ConfigurationSettings.AppSettings(PRODUCT_CODE_LK_ID)
    End Function

    Public Shared Function GetMIMSProductCodeLKID() As Integer
        Return System.Configuration.ConfigurationSettings.AppSettings(MIMS_PRODUCT_CODE_LK_ID)
    End Function

    Public Shared Function GetProductColorLKID() As Integer
        Return System.Configuration.ConfigurationSettings.AppSettings(PRODUCT_COLOR_LK_ID)
    End Function
    Public Shared Function GetFreightForwarderLKID() As Integer
        Return System.Configuration.ConfigurationSettings.AppSettings(FREIGHT_FORWARDER_LK_ID)
    End Function

    Public Shared Function GetCountryLKID() As Integer
        Return System.Configuration.ConfigurationSettings.AppSettings(COUNTRY_LK_ID)
    End Function

    Public Shared Function GetSupplierCodeLKID() As Integer
        Return System.Configuration.ConfigurationSettings.AppSettings(SUPPLIER_CODE_LK_ID)
    End Function

    Public Shared Function NullOrEmpty(ByVal str As String) As Boolean
        Return IsNothing(str) OrElse IsDBNull(str) OrElse str.Trim = String.Empty
    End Function

    Public Shared Function AppRoot() As String
        'Return System.Web.HttpContext.Current.Request.ApplicationPath
        Return System.Configuration.ConfigurationSettings.AppSettings("AppRoot")
    End Function

    Public Shared Function EnumToList(ByVal type As System.Type) As System.Web.UI.WebControls.ListItemCollection

        Dim fields() As FieldInfo = type.GetFields()


        If (fields.Length <= 1) Then
            Return Nothing
        End If

        Dim myListItemCollection As New System.Web.UI.WebControls.ListItemCollection
        Dim i As Integer
        Dim sTemp As String

        For i = 1 To fields.Length - 1
            sTemp = fields(i).Name.Replace("_", " ")
            sTemp = sTemp.Replace("[", "")
            sTemp = sTemp.Replace("]", "")
            myListItemCollection.Add(New System.Web.UI.WebControls.ListItem(sTemp, fields(i).GetValue(fields(i))))
        Next

        Return myListItemCollection
    End Function

    Public Shared Function FormatDate(ByVal d As DateTime) As String
        If d > Date.MinValue Then
            Return d.ToString(GetDateFormat)
        Else
            Return Nothing
        End If
    End Function

    Public Shared Function GetDateFormat() As String
        Return System.Configuration.ConfigurationSettings.AppSettings(DATE_FORMAT_KEY)
    End Function

    Public Shared Function Week(ByVal val As Date) As Integer
        'Dim myCI As New CultureInfo("en-US")
        'Dim myCal As Calendar = myCI.Calendar

        '' Gets the DTFI properties required by GetWeekOfYear.
        'Dim myCWR As CalendarWeekRule = myCI.DateTimeFormat.CalendarWeekRule
        'Dim myFirstDOW As DayOfWeek = myCI.DateTimeFormat.FirstDayOfWeek
        Dim WeekNo As Integer
        'WeekNo = myCal.GetWeekOfYear(val, myCWR, myFirstDOW)
        WeekNo = Microsoft.VisualBasic.DatePart(DateInterval.WeekOfYear, val)
        '' Algorithm used:
        '' From Klaus Tondering's Calendar document (The Authority/Guru)
        '' hhtp://www.tondering.dk/claus/calendar.html
        '' a = (14-month) / 12
        '' y = year + 4800 - a
        '' m = month + 12a - 3
        '' J = day + (153m + 2) / 5 + 365y + y / 4 - y / 100 + y / 400 - 32045
        '' d4 = (J + 31741 - (J mod 7)) mod 146097 mod 36524 mod 1461
        '' L = d4 / 1460
        '' d1 = ((d4 - L) mod 365) + L
        '' WeekNumber = d1 / 7 + 1
        'Dim a As Integer = (14 - val.Month) / 12
        'Dim y As Integer = val.Year + 4800 - a
        'Dim m As Integer = val.Month + 12 * a - 3
        'Dim J As Integer = val.Day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045
        'Dim d4 As Integer = (J + 31741 - (J Mod 7)) Mod 146097 Mod 36524 Mod 1461
        'Dim L As Integer = d4 / 1460
        'Dim d1 As Integer = ((d4 - L) Mod 365) + L
        'Dim WeekNo As Integer = d1 / 7 + 1

        If WeekNo > 52 Then
            WeekNo = WeekNo - 52
        End If

        Return WeekNo
    End Function

    Public Shared Function Year(ByVal val As Date) As Integer
        Dim WeekNo As Integer
        WeekNo = Microsoft.VisualBasic.DatePart(DateInterval.WeekOfYear, val)
        If WeekNo > 52 Then
            Return (val.Year + 1)
        Else
            Return val.Year
        End If
    End Function

    Public Shared Function WebServerName(ByVal request As System.Web.HttpRequest) As String
        If (request.ServerVariables("SERVER_PORT") <> "80") Then
            Return request.ServerVariables("SERVER_NAME") + ":" + request.ServerVariables("SERVER_PORT")
        Else
            Return request.ServerVariables("SERVER_NAME")
        End If
    End Function

    Public Shared Sub ShowMessageBoard(ByVal sMsg As String, ByVal sRedirectUrl As String)
        System.Web.HttpContext.Current.Items("msg") = sMsg
        System.Web.HttpContext.Current.Items("url") = sRedirectUrl

        System.Web.HttpContext.Current.Server.Transfer(AppRoot() + "/Dialogs/MessageBoard.aspx")
    End Sub

    Public Shared Function GetLKDValue(ByVal id As Integer, ByVal valueno As Integer) As String
        If id > 0 Then
            Try
                Dim objLKD As New CLookupData
                objLKD.Load(id)
                Select Case valueno
                    Case 1
                        Return objLKD.Value1
                    Case 2
                        Return objLKD.Value2
                    Case 3
                        Return objLKD.Value3
                End Select
            Catch ex As Exception
            End Try
        Else
            Return Nothing
        End If
    End Function

    Public Shared Function HashPassword(ByVal pwd As String) As String
        If Trim(HashAlgorithm) = vbNullString Then
            Return pwd
        Else
            Return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, HashAlgorithm)
        End If
    End Function

    Public Shared Function C2Dec(ByVal obj As Object) As Decimal
        If IsNothing(obj) OrElse IsDBNull(obj) Then
            Return 0
        Else
            Return CDec(obj)
        End If
    End Function
#End Region
End Class
